package com.jjh.dao.impl;

import com.jjh.winterframework.annatation.Component;
import com.jjh.consts.Properties;
import com.jjh.dao.DishDao;
import com.jjh.pojo.Dishes;
import com.jjh.util.FileUtils;
import com.jjh.util.PageBody;
import com.jjh.wrapper.QueryWrapper;

import java.util.List;
@Component
public class DishDaoImpl implements DishDao {

    /**
     * 分页查询菜品信息
     * @param page 页数
     * @param size 尺寸
     * @return 菜品集合
     */
    @Override
    public PageBody page(int page, int size, QueryWrapper wrapper) {


        List<Object> dishlist =
                FileUtils.pageQueryList(page,size,Properties.dishPath,Dishes.class,wrapper);

        PageBody pageBody = new PageBody();
        try {
            int total = dishlist.size();
            pageBody.setCurrntPage(page);
            pageBody.setSize(size);
            pageBody.setTotal(total);
            if(size!=0){
                pageBody.setPageSize(total/size);
            }
            pageBody.setResults(dishlist);
        }catch (Exception e){
            e.printStackTrace();
        }

        return pageBody;
    }

    /**
     * 根据菜品id扣除库存
     * @param dishes
     */
    @Override
    public void removeCount(Dishes dishes) {

        FileUtils.updateOne(Properties.dishPath,dishes);

    }
}
